package cn.edu.tju.elm.model;

import cn.edu.tju.core.model.BaseEntity;
import cn.edu.tju.core.model.User;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 银行卡交易记录实体类
 */
@Entity
@Table(name = "bank_card_transactions")
public class BankCardTransaction extends BaseEntity {

    /**
     * 关联的银行卡
     */
    @ManyToOne
    @JoinColumn(name = "bank_card_id", nullable = false)
    @NotNull
    private BankCard bankCard;

    /**
     * 关联的用户
     */
    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    @NotNull
    private User user;

    /**
     * 交易类型（1-充值，2-消费，3-退款，4-转账）
     */
    @Column(name = "transaction_type", nullable = false)
    @NotNull
    private Integer transactionType;

    /**
     * 交易金额
     */
    @Column(name = "amount", precision = 15, scale = 2, nullable = false)
    @NotNull
    private BigDecimal amount;

    /**
     * 交易前余额
     */
    @Column(name = "balance_before", precision = 15, scale = 2)
    private BigDecimal balanceBefore;

    /**
     * 交易后余额
     */
    @Column(name = "balance_after", precision = 15, scale = 2)
    private BigDecimal balanceAfter;

    /**
     * 交易状态（1-成功，2-失败，3-处理中）
     */
    @Column(name = "status", nullable = false)
    @NotNull
    private Integer status = 1;

    /**
     * 交易时间
     */
    @Column(name = "transaction_time", nullable = false)
    @NotNull
    private LocalDateTime transactionTime;

    /**
     * 关联订单ID（如果是订单支付）
     */
    @Column(name = "order_id")
    private Long orderId;

    /**
     * 交易描述
     */
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 交易流水号
     */
    @Column(name = "transaction_no", length = 50, unique = true)
    private String transactionNo;

    /**
     * 备注
     */
    @Column(name = "remark", length = 500)
    private String remark;

    // 构造函数
    public BankCardTransaction() {
        this.transactionTime = LocalDateTime.now();
    }

    // Getter和Setter方法
    public BankCard getBankCard() {
        return bankCard;
    }

    public void setBankCard(BankCard bankCard) {
        this.bankCard = bankCard;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Integer getTransactionType() {
        return transactionType;
    }

    public void setTransactionType(Integer transactionType) {
        this.transactionType = transactionType;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public BigDecimal getBalanceBefore() {
        return balanceBefore;
    }

    public void setBalanceBefore(BigDecimal balanceBefore) {
        this.balanceBefore = balanceBefore;
    }

    public BigDecimal getBalanceAfter() {
        return balanceAfter;
    }

    public void setBalanceAfter(BigDecimal balanceAfter) {
        this.balanceAfter = balanceAfter;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public LocalDateTime getTransactionTime() {
        return transactionTime;
    }

    public void setTransactionTime(LocalDateTime transactionTime) {
        this.transactionTime = transactionTime;
    }

    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTransactionNo() {
        return transactionNo;
    }

    public void setTransactionNo(String transactionNo) {
        this.transactionNo = transactionNo;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * 获取交易类型描述
     */
    public String getTransactionTypeDesc() {
        switch (transactionType) {
            case 1: return "充值";
            case 2: return "消费";
            case 3: return "退款";
            case 4: return "转账";
            default: return "未知";
        }
    }

    /**
     * 获取交易状态描述
     */
    public String getStatusDesc() {
        switch (status) {
            case 1: return "成功";
            case 2: return "失败";
            case 3: return "处理中";
            default: return "未知";
        }
    }

    @Override
    public String toString() {
        return "BankCardTransaction{" +
                "id=" + getId() +
                ", transactionType=" + transactionType +
                ", amount=" + amount +
                ", status=" + status +
                ", transactionTime=" + transactionTime +
                ", transactionNo='" + transactionNo + '\'' +
                '}';
    }
}

